﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Veza typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class VezaDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<VezaDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Veza _currentVeza = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxVezaDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxVezaErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxVezaBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IDParent property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDParentDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDChild property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDChildDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Hierarchy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxHierarchyDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Lvl property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLvlDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the HierarchyComputed property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxHierarchyComputedDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Dokument> _IDChildList;
		
		/// <summary> 
		/// The list of selectable Dokument
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Dokument> IDChildList
		{
			get {return this._IDChildList;}
			set 
			{
				this._IDChildList = value;
				this.uxIDChildDataGridViewColumn.DataSource = null;
				this.uxIDChildDataGridViewColumn.DataSource = this._IDChildList;
			}
		}
		
		private bool _allowNewItemInIDChildList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Dokument
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDChildList
		{
			get { return _allowNewItemInIDChildList;}
			set
			{
				this._allowNewItemInIDChildList = value;
				this.uxIDChildDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Dokument> _IDParentList;
		
		/// <summary> 
		/// The list of selectable Dokument
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Dokument> IDParentList
		{
			get {return this._IDParentList;}
			set 
			{
				this._IDParentList = value;
				this.uxIDParentDataGridViewColumn.DataSource = null;
				this.uxIDParentDataGridViewColumn.DataSource = this._IDParentList;
			}
		}
		
		private bool _allowNewItemInIDParentList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Dokument
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDParentList
		{
			get { return _allowNewItemInIDParentList;}
			set
			{
				this._allowNewItemInIDParentList = value;
				this.uxIDParentDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Veza> _VezaList;
				
		/// <summary> 
		/// The list of Veza to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Veza> VezaList
		{
			get {return this._VezaList;}
			set
			{
				this._VezaList = value;
				this.uxVezaBindingSource.DataSource = null;
				this.uxVezaBindingSource.DataSource = value;
				this.uxVezaDataGridView.DataSource = null;
				this.uxVezaDataGridView.DataSource = this.uxVezaBindingSource;				
				//this.uxVezaBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxVezaBindingSource_ListChanged);
				this.uxVezaBindingSource.CurrentItemChanged += new System.EventHandler(OnVezaBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnVezaBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentVeza = uxVezaBindingSource.Current as Entities.Veza;
			
			if (_currentVeza != null)
			{
				_currentVeza.Validate();
			}
			//_Veza.Validate();
			OnCurrentEntityChanged();
		}

		//void uxVezaBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Veza"/> instance.
		/// </summary>
		public Entities.Veza SelectedVeza
		{
			get {return this._currentVeza;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxVezaDataGridView.VirtualMode;}
			set
			{
				this.uxVezaDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxVezaDataGridView.AllowUserToAddRows;}
			set {this.uxVezaDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxVezaDataGridView.AllowUserToDeleteRows;}
			set {this.uxVezaDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxVezaDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxVezaDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="VezaDataGridViewBase"/> class.
		/// </summary>
		public VezaDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxVezaDataGridView = new System.Windows.Forms.DataGridView();
			this.uxVezaBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxVezaErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDParentDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDChildDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxHierarchyDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLvlDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxHierarchyComputedDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDChildBindingSource = new DokumentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDChildBindingSource)).BeginInit();
			//this.uxIDParentBindingSource = new DokumentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDParentBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxVezaErrorProvider
			// 
			this.uxVezaErrorProvider.ContainerControl = this;
			this.uxVezaErrorProvider.DataSource = this.uxVezaBindingSource;						
			// 
			// uxVezaDataGridView
			// 
			this.uxVezaDataGridView.AutoGenerateColumns = false;
			this.uxVezaDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxVezaDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDParentDataGridViewColumn,
		this.uxIDChildDataGridViewColumn,
		this.uxHierarchyDataGridViewColumn,
		this.uxLvlDataGridViewColumn,
		this.uxHierarchyComputedDataGridViewColumn			});
			this.uxVezaDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxVezaDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxVezaDataGridView.Name = "uxVezaDataGridView";
			this.uxVezaDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxVezaDataGridView.TabIndex = 0;	
			this.uxVezaDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxVezaDataGridView.EnableHeadersVisualStyles = false;
			this.uxVezaDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnVezaDataGridViewDataError);
			this.uxVezaDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVezaDataGridViewCellValueNeeded);
			this.uxVezaDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVezaDataGridViewCellValuePushed);
			
			//
			// uxIDParentDataGridViewColumn
			//
			this.uxIDParentDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDParentDataGridViewColumn.DataPropertyName = "IDParent";
			this.uxIDParentDataGridViewColumn.HeaderText = "IDParent";
			this.uxIDParentDataGridViewColumn.Name = "uxIDParentDataGridViewColumn";
			this.uxIDParentDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDParentDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDParentDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDChildDataGridViewColumn
			//
			this.uxIDChildDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDChildDataGridViewColumn.DataPropertyName = "IDChild";
			this.uxIDChildDataGridViewColumn.HeaderText = "IDChild";
			this.uxIDChildDataGridViewColumn.Name = "uxIDChildDataGridViewColumn";
			this.uxIDChildDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDChildDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDChildDataGridViewColumn.ReadOnly = false;		
			//
			// uxHierarchyDataGridViewColumn
			//
			this.uxHierarchyDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxHierarchyDataGridViewColumn.DataPropertyName = "Hierarchy";
			this.uxHierarchyDataGridViewColumn.HeaderText = "Hierarchy";
			this.uxHierarchyDataGridViewColumn.Name = "uxHierarchyDataGridViewColumn";
			this.uxHierarchyDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxHierarchyDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxHierarchyDataGridViewColumn.ReadOnly = false;		
			//
			// uxLvlDataGridViewColumn
			//
			this.uxLvlDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLvlDataGridViewColumn.DataPropertyName = "Lvl";
			this.uxLvlDataGridViewColumn.HeaderText = "Lvl";
			this.uxLvlDataGridViewColumn.Name = "uxLvlDataGridViewColumn";
			this.uxLvlDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLvlDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLvlDataGridViewColumn.ReadOnly = true;		
			//
			// uxHierarchyComputedDataGridViewColumn
			//
			this.uxHierarchyComputedDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxHierarchyComputedDataGridViewColumn.DataPropertyName = "HierarchyComputed";
			this.uxHierarchyComputedDataGridViewColumn.HeaderText = "HierarchyComputed";
			this.uxHierarchyComputedDataGridViewColumn.Name = "uxHierarchyComputedDataGridViewColumn";
			this.uxHierarchyComputedDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxHierarchyComputedDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxHierarchyComputedDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDChildDataGridViewColumn
			//				
			this.uxIDChildDataGridViewColumn.DisplayMember = "IDDokumentTip";	
			this.uxIDChildDataGridViewColumn.ValueMember = "ID";	
			this.uxIDChildDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDChildDataGridViewColumn.DataSource = uxIDChildBindingSource;				
				
			//
			// uxIDParentDataGridViewColumn
			//				
			this.uxIDParentDataGridViewColumn.DisplayMember = "IDDokumentTip";	
			this.uxIDParentDataGridViewColumn.ValueMember = "ID";	
			this.uxIDParentDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDParentDataGridViewColumn.DataSource = uxIDParentBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxVezaDataGridView);
			this.Name = "VezaDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDChildBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDParentBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVezaBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				VezaDataGridViewEventArgs args = new VezaDataGridViewEventArgs();
				args.Veza = _currentVeza;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class VezaDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Veza	_Veza;
	
			/// <summary>
			/// the  Entities.Veza instance.
			/// </summary>
			public Entities.Veza Veza
			{
				get { return _Veza; }
				set { _Veza = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxVezaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnVezaDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxVezaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVezaDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVezaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDParentDataGridViewColumn":
						e.Value = VezaList[e.RowIndex].IDParent;
						break;
					case "uxIDChildDataGridViewColumn":
						e.Value = VezaList[e.RowIndex].IDChild;
						break;
					case "uxHierarchyDataGridViewColumn":
						e.Value = VezaList[e.RowIndex].Hierarchy;
						break;
					case "uxLvlDataGridViewColumn":
						e.Value = VezaList[e.RowIndex].Lvl;
						break;
					case "uxHierarchyComputedDataGridViewColumn":
						e.Value = VezaList[e.RowIndex].HierarchyComputed;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxVezaDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVezaDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVezaDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDParentDataGridViewColumn":
						VezaList[e.RowIndex].IDParent = (System.Int32)e.Value;
						break;
					case "uxIDChildDataGridViewColumn":
						VezaList[e.RowIndex].IDChild = (System.Int32)e.Value;
						break;
					case "uxHierarchyDataGridViewColumn":
						VezaList[e.RowIndex].Hierarchy = (System.String)e.Value;
						break;
					case "uxLvlDataGridViewColumn":
						VezaList[e.RowIndex].Lvl = (System.Int32?)e.Value;
						break;
					case "uxHierarchyComputedDataGridViewColumn":
						VezaList[e.RowIndex].HierarchyComputed = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
